#ifndef GDRAWER_H
#define GDRAWER_H

#include <qpixmap.h>
#include <qwidget.h>
#include <vector>
#include <utility>

class QToolButton;
class PlotSettings;
class drawer;

class drawer : public QWidget
{
    Q_OBJECT

    void updateRubberBandRegion_();
    void drawGrid_(QPainter * painter);
    void drawPoints_(QPainter * painter);

    QToolButton * zoomInB_;
    QToolButton * zoomOB_;
    std::pair<double, double> * points_;
    int curZoom_;
    unsigned long long n_;
    std::vector<PlotSettings> zoomStack_;
    bool rubberBandIsShown;
    QRect rubberBandRect;
    QPixmap pixmap_;
    enum { Margin = 40 };
    bool mode;

protected:
    void paintEvent(QPaintEvent *event);
    void resizeEvent(QResizeEvent *event);
    void mousePressEvent(QMouseEvent * event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);
    void wheelEvent(QWheelEvent *event);

public:

    drawer(QWidget *parent = 0, Qt::WFlags flags = 0);
    void setSettings(const PlotSettings &settings);
    void setData(std::pair<double, double> * points, unsigned long long pcount, bool);
    QSize minimumSizeHint() const;
    QSize sizeHint() const;

public slots:
    void zoomIn();
    void zoomOut();
    void data(std::pair<double, double> * points, unsigned long long pcount, bool);
    void importSettings(const PlotSettings &);
};

class PlotSettings
{
    void adjustAxis(double &min, double &max, int &numTicks);

public: 
  PlotSettings();
  
  void scroll(int dx_, int dy); 
  void adjust(); 
  double spanX() const { return maxX - minX; } 
  double spanY() const { return maxY - minY; } 
  
  double minX; 
  double maxX; 
  int numXTicks; 
  double minY; 
  double maxY; 
  int numYTicks;
};

#endif